%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This MATLAB code generates Figure 9 (based on data preparation in STATA)
% of the paper "Process or Candidate: The International Community and the 
% Demand for Electoral Integrity". 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear
clf

%Create a dataset array (works in Matlab 2012a, newer versions should use
%table instead)
data = dataset('File','analysis_table.csv','Delimiter',',');
summary(data)

%Create a grouped bar chart
hold on
%Creat grouped bar chart
bar_handle= bar(data.polarization(1:3), [data.mean_gaininc(data.warusa==0) data.mean_gaininc(data.warusa==1)], 0.75, 'grouped');
set(bar_handle(1),'FaceColor',[0.5 0.5 0.5]) %this would match the colors
set(bar_handle(2),'FaceColor','w') %this would match the colors
set(gca,'XTick',data.polarization(1:3))
set(gca,'YLim',[0 1])
xlabel('Polarization');
ylabel('Incumbent Vote Gain');
legend('Election Hegemon', 'Election War','Location','northwest');

%Add two line plots 
x = data.polarization(1:3)-0.143
y = data.mean_gaininc(data.warusa==0)
plot(x,y,'-ks')
x = data.polarization(1:3)+0.143
y = data.mean_gaininc(data.warusa==1)
plot(x,y,':ko')
hold off

print('matlab_main_body/figure9a','-dpng')

clf

%Create a grouped bar chart
hold on
%Creat grouped bar chart
bar_handle= bar(data.polarization(1:3), [data.mean_concern(data.warusa==0) data.mean_concern(data.warusa==1)], 0.75, 'grouped');
set(bar_handle(1),'FaceColor',[0.5 0.5 0.5]) %this would match the colors
set(bar_handle(2),'FaceColor','w') %this would match the colors
set(gca,'XTick',data.polarization(1:3))
set(gca,'YLim',[0 1])
%title('Bias by polarization');
xlabel('Polarization');
ylabel('Bias');
legend('Election Hegemon', 'Election War','Location','northeast');
%Add two line plots
x = data.polarization(1:3)-0.143
y = data.mean_concern(data.warusa==0)
plot(x,y,'-ks')
x = data.polarization(1:3)+0.143
y = data.mean_concern(data.warusa==1)
plot(x,y,':ko')
hold off

print('matlab_main_body/figure9b','-dpng')

clf
